# topic/views.py

from django.shortcuts import render, redirect
from .import models
from django.http import HttpResponseRedirect
from django.views.generic.base import View
from django.urls import reverse
from operation.models import LabelProfile
# Create your views here.


class IndexView(View):
    '''首页'''

    def get(self, request):
        topics = models.TopicProfile.objects.all().order_by('-published_time')
        labels = LabelProfile.objects.all()
        return render(request, 'index.html', {'topics': topics, 'labels': labels})


class Test(View):
    '''测试'''
    def get(self, request):
        return render(request, 'personal-center.html', {})


class HrefView(View):
    '''文章链接'''
    def get(self, request, topic_id):
        topic = models.TopicProfile.objects.get(pk=topic_id)
        topic.topic_reading_quantity += 1
        topic.save()
        labels = topic.topic_label.all()
        topic_comments = models.topic_comment_profile.objects.filter(topic_id=topic_id).order_by('-topic_comment_published_time')
        topic_respects = models.topic_respect_profile.objects.all().order_by('-topic_respect_published_time')
        return render(request, 'topic.html', {'topic': topic, 'labels': labels,
                                              'topic_comments': topic_comments, 'topic_respects': topic_respects})


def comment_action(request):
    '''评论'''
    user = request.user
    topic_comment_content = request.POST.get('topic_comment', None)
    topic_id = request.POST.get('topic_id', None)
    topic = models.TopicProfile.objects.get(pk=topic_id)
    models.topic_comment_profile.objects.create(userprofile_id=user, topic_id=topic, topic_comment_content=topic_comment_content)
    referer = request.META.get('HTTP_REFERER', reverse('topic:index'))
    return redirect(referer)


def respect_action(request):
    '''评论的回复'''
    user = request.user
    topic_respect_content = request.POST.get('topic_respect', None)
    topic_comment_id = request.POST.get('topic_comment_id', None)
    topic_comment = models.topic_comment_profile.objects.get(pk=topic_comment_id)
    comment_userprofile = request.POST.get('comment_userprofile_id', None)
    if comment_userprofile == '':
        models.topic_respect_profile.objects.create(userprofile_id=user,
                                                    topic_respect_profile_id=topic_comment,
                                                    topic_respect_content=topic_respect_content)
    else:
        comment_userprofile_id = models.UserProfile.objects.get(username=comment_userprofile)
        models.topic_respect_profile.objects.create(userprofile_id=user, comment_userprofile_id=comment_userprofile_id, topic_respect_profile_id=topic_comment, topic_respect_content=topic_respect_content)
    topic_comment.topic_comment_comment_num += 1
    topic_comment.save()
    referer = request.META.get('HTTP_REFERER', reverse('topic:index'))
    return redirect(referer)


class NewTopicView(View):
    '''新文章'''
    def get(self, request):
        labels = LabelProfile.objects.all()
        return render(request, 'edit-topic.html', {'labels': labels})

    def post(self, request):
        user = request.user
        topic_title = request.POST.get('title', None)
        topic_content = request.POST.get('content', None)
        labellist = request.POST.getlist("label")
        label = models.LabelProfile.objects.all()
        newtopic = models.TopicProfile.objects.create(userprofile_id=user, topic_title=topic_title, topic_content=topic_content)
        for i in label:
            if i.label_content in labellist:
                newtopic.topic_label.add(i)
        return HttpResponseRedirect(reverse('topic:index'))



